package com;
import com.tool.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.tool.jdbctool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Users {
    private Integer Id;
    private String username;
    private String password;
    private String sex;
    private String email;
    public Integer getId() {
        return Id;
    }
    public void setId(Integer id) {
        Id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Users() {
    }
    public Users(String username, String password, String sex, String email) {
        this.username = username;
        this.password = password;
        this.sex = sex;
        this.email = email;
    }

    public Users(Integer id, String username, String password, String sex, String email) {
        Id = id;
        this.username = username;
        this.password = password;
        this.sex = sex;
        this.email = email;
    }

    public void save(){
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        try {
            connection=jdbctool.getconection();
            statement=connection.prepareStatement(
           "insert into users(`username`,`password`,`sex`,`email`) values(?,?,?,?)");
            statement.setString(1,username);
            statement.setString(2,password);
            statement.setString(3,sex);
            statement.setString(4,email);
            statement.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            jdbctool.close(connection,statement,resultSet);
        }

    }
    public List<Users> find(){
        ArrayList<Users> list=new ArrayList<Users>();
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        try {
            connection=jdbctool.getconection();
            statement=connection.prepareStatement(
                    "select * from users");

            resultSet=statement.executeQuery();
            while (resultSet.next())
            {
                Users users=new Users(resultSet.getInt(1),resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getString(4),
                        resultSet.getString(5));
                list.add(users);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            jdbctool.close(connection,statement,resultSet);
        }
        return list;
    }
    public int delete(String i){
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        int num=0;
        try {
            connection=jdbctool.getconection();
            statement=connection.prepareStatement(
                    "delete from `users`where `userid`=?");
            statement.setString(1,i);
            num=statement.executeUpdate();
            System.out.println("sc");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            jdbctool.close(connection,statement,resultSet);
        }
        return num;
    }
    public int denlu(String a,String b){
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        int num=0;
        try {
            connection=jdbctool.getconection();
            System.out.println(a+b);
            statement=connection.prepareStatement(
                    "select * from `users`where `username`=?and password=?");
            statement.setString(1,a);
            statement.setString(2,b);
            resultSet=statement.executeQuery();
            while (resultSet.next())
                num=1;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            jdbctool.close(connection,statement,resultSet);
        }
        System.out.println(num);
        return num;
    }
}
